package com.zenithmind.chat.config;

import com.zenithmind.chat.service.AiChatPermissionService;
import com.zenithmind.chat.service.AiChatValidationService;
import com.zenithmind.chat.service.AiChatCacheService;
import com.zenithmind.chat.service.AiChatStreamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * AI聊天配置验证器
 * 遵循单一职责原则，专门负责启动时的配置验证
 */
@Slf4j
@Component
public class AiChatConfigValidator {
    
    private final AiChatPermissionService permissionService;
    private final AiChatValidationService validationService;
    private final AiChatCacheService cacheService;
    private final AiChatStreamService streamService;
    
    public AiChatConfigValidator(AiChatPermissionService permissionService,
                                AiChatValidationService validationService,
                                AiChatCacheService cacheService,
                                AiChatStreamService streamService) {
        this.permissionService = permissionService;
        this.validationService = validationService;
        this.cacheService = cacheService;
        this.streamService = streamService;
    }
    
    /**
     * 应用启动后验证配置
     */
    @EventListener(ApplicationReadyEvent.class)
    public void validateConfiguration() {
        log.info("开始验证AI聊天服务配置...");
        
        try {
            // 验证服务依赖
            validateServiceDependencies();
            
            // 验证缓存服务
            validateCacheService();
            
            // 验证流式服务
            validateStreamService();
            
            log.info("AI聊天服务配置验证完成，所有服务正常");
            
        } catch (Exception e) {
            log.error("AI聊天服务配置验证失败", e);
            throw new RuntimeException("AI聊天服务配置验证失败: " + e.getMessage(), e);
        }
    }
    
    /**
     * 验证服务依赖
     */
    private void validateServiceDependencies() {
        if (permissionService == null) {
            throw new IllegalStateException("权限服务未正确初始化");
        }
        
        if (validationService == null) {
            throw new IllegalStateException("验证服务未正确初始化");
        }
        
        log.debug("服务依赖验证通过");
    }
    
    /**
     * 验证缓存服务
     */
    private void validateCacheService() {
        if (cacheService == null) {
            throw new IllegalStateException("缓存服务未正确初始化");
        }
        
        // 测试缓存功能
        try {
            cacheService.evictAllCache();
            log.debug("缓存服务验证通过");
        } catch (Exception e) {
            throw new IllegalStateException("缓存服务功能异常: " + e.getMessage(), e);
        }
    }
    
    /**
     * 验证流式服务
     */
    private void validateStreamService() {
        if (streamService == null) {
            throw new IllegalStateException("流式服务未正确初始化");
        }
        
        // 测试SSE创建功能
        try {
            var emitter = streamService.createSseEmitter();
            if (emitter == null) {
                throw new IllegalStateException("SSE创建功能异常");
            }
            streamService.completeSse(emitter);
            log.debug("流式服务验证通过");
        } catch (Exception e) {
            throw new IllegalStateException("流式服务功能异常: " + e.getMessage(), e);
        }
    }
}
